/*
 * Copyright 2020 Netflix, Inc.
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package com.netflix.zuul.monitoring;

import static org.assertj.core.api.Assertions.assertThat;

import com.netflix.spectator.api.DefaultRegistry;
import com.netflix.spectator.api.Gauge;
import com.netflix.spectator.api.Registry;
import com.netflix.zuul.Attrs;
import com.netflix.zuul.netty.server.Server;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.jupiter.api.Test;

class ConnCounterTest {
    @Test
    void record() {
        EmbeddedChannel chan = new EmbeddedChannel();
        Attrs attrs = Attrs.newInstance();
        chan.attr(Server.CONN_DIMENSIONS).set(attrs);
        Registry registry = new DefaultRegistry();
        ConnCounter counter = ConnCounter.install(chan, registry, registry.createId("foo"));

        counter.increment("start");
        counter.increment("middle");
        Attrs.newKey("bar").put(attrs, "baz");
        counter.increment("end");

        Gauge meter1 = registry.gauge(registry.createId("foo.start", "from", "nascent"));
        assertThat(meter1).isNotNull();
        assertThat(meter1.value()).isCloseTo(1.0, org.assertj.core.data.Offset.offset(0.0));

        Gauge meter2 = registry.gauge(registry.createId("foo.middle", "from", "start"));
        assertThat(meter2).isNotNull();
        assertThat(meter2.value()).isCloseTo(1.0, org.assertj.core.data.Offset.offset(0.0));

        Gauge meter3 = registry.gauge(registry.createId("foo.end", "from", "middle", "bar", "baz"));
        assertThat(meter3).isNotNull();
        assertThat(meter3.value()).isCloseTo(1.0, org.assertj.core.data.Offset.offset(0.0));
    }

    @Test
    void activeConnsCount() {
        EmbeddedChannel channel = new EmbeddedChannel();
        Attrs attrs = Attrs.newInstance();
        channel.attr(Server.CONN_DIMENSIONS).set(attrs);
        Registry registry = new DefaultRegistry();

        ConnCounter.install(channel, registry, registry.createId("foo"));

        // Dedup increments
        ConnCounter.from(channel).increment("active");
        ConnCounter.from(channel).increment("active");

        assertThat(ConnCounter.from(channel).getCurrentActiveConns())
                .isCloseTo(1.0, org.assertj.core.data.Offset.offset(0.0));
    }
}
